package com.kaifamiao.N0801;

import java.util.Arrays;
import java.util.Scanner;

public class Bank {

    // 供所有的实例方法调用
    private Scanner sc = new Scanner(System.in);
    // 存账户信息的数组
    private Account[] accounts = new Account[10];
    // 已经有多少账户
    private int count = 0;

    // 当前登录用户
    private Account current;

    //系统入口
    public void start() {
        System.out.println("欢迎来到 开发喵 银行");
        // 初始化一个账户
        Account account = new Account();
        account.setCardId(10000);
        account.setPassword("1111");
        account.setName("幸运儿");

        accounts[count] = account;
        count ++;

        oneMenu();
    }

    //一级菜单
    private void oneMenu(){
        System.out.println("请选择您要办理业务：(输入对应的编号)");
        System.out.println(" 1. 开户");
        System.out.println(" 2. 登录");
        System.out.println(" 0. 退出");

        // 用户输入要执行功能
        int selector = sc.nextInt();
        selectOneMenu(selector);
    }

    //菜单
    private void menu(){
        System.out.println("请选择您要办理业务：(输入对应的编号)");

        System.out.println(" 2. 存钱");
        System.out.println(" 3. 取钱");
        System.out.println(" 4. 查询余额");
        System.out.println(" 5. 查询账户信息");
        System.out.println(" 6. 转账");
        System.out.println(" 7. 查看所有账户信息【为了我们方便】");
        System.out.println(" 0. 退出");
        // 用户输入要执行功能
        int selector = sc.nextInt();
        selectMenu(selector);
    }


    private void selectOneMenu(int selector){
        switch (selector){
            // 开户
            case 1:
                create();
                break;
            // 登录
            case 2:
                login();
                break;
            // 退出
            case 0:
                exit();
                break;
        }
    }

    //登录
    private void login() {
        System.out.println("请输入卡号：");
        //long cardId = sc.nextLong();
        String name = sc.next();
        System.out.println("请输入密码：");
        String password = sc.next();
        //Account account = findAccount(cardId, password);
        Account account = findAccount(Long.parseLong(name), password);

        if (account == null){
            System.err.println("用户名或密码错误，请重试!");
            oneMenu();
        } else {
            System.out.println("登录成功!");
            // 记录一下当前登录的账户是谁
            current = account;
            // 继续操作
            while(true){
                // 显示菜单
                menu();
            }
        }
    }

    //根据卡号和密码查找账户
    private Account findAccount(long cardId, String password){
        for (int i = 0; i < count; i ++){
            Account account = accounts[i];
            if (account.getCardId() == cardId && password.equals(account.getPassword())){
                return account;
            }
        }
        return null;
    }

    //根据用户的输入选择要执行的功能
    private void selectMenu(int selector){
        switch (selector){
            // 开户
            case 1:
                create();
                break;
            // 存钱
            case 2:
                saveMoney();
                break;
            // 取钱
            case 3:
                takeMoney();
                break;
            // 查询余额
            case 4:
                getMoney();
                break;
            // 查询账户信息
            case 5:
                getAccount();
                break;
            // 转账
            case 6:
                transfer();
                break;
            // 查看所有账户信息
            case 7:
                showAll();
                break;
            // 退出
            case 0:
                exit();
                break;
        }
    }

    //查看所有账户信息
    private void showAll() {
        if (count > 0){
            System.err.println("现有 " + count + " 个账户.");
            for (int i = 0; i < count; i ++){
                accounts[i].show();
            }
        } else {
            System.err.println("还没有客户");
        }

    }

    //退出
    private void exit() {
        System.out.println("欢迎下次光临~");
        System.exit(0);
    }

    //转账
    private void transfer() {
        System.out.println("请输入对方卡号：");
        long to = sc.nextLong();
        // 查看卡号是否存在
        Account target = valCard(to);
        if (target != null){
            System.out.println("请输入转账金额：");
            double money = sc.nextDouble();
            // 账户中是否有足够的钱
            if (current.getMoney() > money){
                // 当前账户的钱
                current.setMoney(current.getMoney() - money);
                // 收账方
                target.setMoney(target.getMoney() + money);
                System.out.println("转账成功!");
            } else{
                System.err.println("账户余额不足，转账失败");
            }
        } else{
            System.err.println("卡号输入错误!");
        }
    }

    private Account valCard(long cardId){
        for (int i = 0; i < count; i ++){
            Account account = accounts[i];
            if (account.getCardId() == cardId){
                return account;
            }
        }
        return null;
    }

    //查询账户信息
    private void getAccount() {
        System.out.println(current);
    }

    //取钱
    private void takeMoney() {
        System.out.println("请输入想取出的金额");
        double money = sc.nextDouble();
        current.setMoney(current.getMoney() - money);
    }

    //查询余额
    private void getMoney() {
        System.out.println("您的金额为：" + current.getMoney());
    }

    //存钱
    private void saveMoney() {
        System.out.println("请输入想存储的金额");
        double money = sc.nextDouble();
        current.setMoney(current.getMoney() + money);
    }

    //开户
    private void create(){
        // 没开一个账户都是在创建一个 Account 对象
        Account account = new Account();
        System.out.println("请输入开户人姓名：");
        String name = sc.next();
        System.out.println("请输入密码：");
        String password = sc.next();

        // 生成卡号
        long cardId = System.currentTimeMillis();
        account.setName(name);
        account.setPassword(password);
        account.setCardId(cardId);

        // 开户送钱
        account.setMoney(8888);

        // 将创建的账户存到 数组中
        // 判断数组中还有空间吗？ 已有的账户数量 等于 最大能存的账户数量
        if (count == accounts.length){
            // 扩容，把存储账户信息的数组扩大, 把容量 翻倍
            int newSize = accounts.length * 2;
            accounts = Arrays.copyOf(accounts, newSize);
        }
        accounts[count] = account;
        count ++; // 人数 +1

        System.out.println("您的卡号是：" + cardId);
        oneMenu();
    }
}









